<?php

/**
 * 
 * Provides authentication of user supplied credentials against the Netminds account info store
 */
class Blogger_Auth_Adapter implements Zend_Auth_Adapter_Interface
{
	/**
	 * 
	 * username of the user trying to authenticate
	 * @var string
	 */
	private $username;
	/**
	 * 
	 * password of the user trying to authenticate
	 * @var string
	 */
	private $password;
	
	/**
	 * 
	 * Sets up the username & password of the user trying to authticate
	 * @param string $username username to be used for authentication
	 * @param string $password password to be used for authentication
	 * @return void
	 */
	function __construct($username, $password)
	{
		$this->username = $username;
		$this->password = $password;
	}
	
	/**
	 * (non-PHPdoc)
	 * @see netminds.com/library/Zend/Auth/Adapter/Zend_Auth_Adapter_Interface::authenticate()
	 */
	public function authenticate()
	{				
		$users = new Blogger_Users();
		$result = $users->getUserByEmail($this->username);				
		if(empty($result))
			return new Zend_Auth_Result(Zend_Auth_Result::FAILURE_IDENTITY_NOT_FOUND, array('username' => $this->username));					
		if($result['password'] != Blogger_Auth::generatePasswordSignature($this->password))
			return new Zend_Auth_Result(Zend_Auth_Result::FAILURE_CREDENTIAL_INVALID, array('username' => $this->username));
		else
		{				
			//TODO remove any previous sessions OR do we allow multiple simulatneous logins?
			$identity = array('id' => $result['id'],
								'fname' => $result['fname'],
								'lname' => $result['lname'],								
								'email' => $result['email'],	//which is same as username right now
								'password' => $result['password'],																		
								);						
			Zend_Session::regenerateId();		//use new id for this new session, to differentiate b/w different users logging in from the same browser window/instance one-after-another
			return new Zend_Auth_Result(Zend_Auth_Result::SUCCESS, $identity);
		}					
	}
	
	
}		//end of class
